local skel = fk.CreateSkill{
  name = "emo__yushi",
  tags = {Skill.Switch},
}

Fk:loadTranslationTable{
  ["emo__yushi"] = "驭时",
  [":emo__yushi"] = "转换技，你可以：阳：将一张牌当“韶光”中的任意基本牌或普通锦囊牌使用；阴：将一张“韶光”置入弃牌堆，然后视为使用一张同名牌。",

  [":emo__yushi_yang"] = "转换技，你可以：<font color='#E0DB2F'>阳：将一张牌当“韶光”中的任意基本牌或普通锦囊牌使用；</font>阴：将一张“韶光”置入弃牌堆，然后视为使用一张同名牌。",
  [":emo__yushi_yin"] = "转换技，你可以：阳：将一张牌当“韶光”中的任意基本牌或普通锦囊牌使用；<font color='#E0DB2F'>阴：将一张“韶光”置入弃牌堆，然后视为使用一张同名牌。</font>",

  ["#emo__yushi-yang"] = "驭时：将一张牌当“韶光”中的任意基本牌或普通锦囊牌使用",
  ["#emo__yushi-yin"] = "驭时：将一张“韶光”置入弃牌堆，视为使用一张同名牌(仅限基本牌、普通锦囊牌)",
  ["#emo__yushi-use"] = "驭时：请选择【%arg】的目标并使用",

  ["$emo__yushi1"] = "天怜苍生、诸邪皆除！",
  ["$emo__yushi2"] = "回应……清平之愿！",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = ".|.|.|.|.|trick,basic",
  mute_card = true,
  prompt = function (self, player)
    return "#emo__yushi-" .. player:getSwitchSkillState(skel.name, false, true)
  end,
  card_num = 1,
  expand_pile = function (self, player)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYin then return player:getPile("$emo__shaoguang") end
    return {}
  end,
  interaction = function(self, player)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYin then return nil end -- 阴状态不开启选择框
    local names = {}
    for _, id in ipairs(player:getPile("$emo__shaoguang")) do
      local card = Fk:getCardById(id)
      if card.type == Card.TypeBasic or card:isCommonTrick() then
        table.insert(names, card.name)
      end
    end
    names = player:getViewAsCardNames(skel.name, names)
    return UI.CardNameBox { choices = names }
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang and self.interaction.data then
      local card = Fk:cloneCard(self.interaction.data)
      card:addSubcard(cards[1])
      card.skillName = skel.name
      return card
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected > 0 then return false end
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      return table.contains(player:getCardIds("he"), to_select)
    else
      return table.contains(player:getPile("$emo__shaoguang"), to_select)
    end
  end,
  feasible = function (self, player, selected, selected_cards, card)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      return false -- 阳状态不走此判定，直接返回假
    else
      return #selected_cards == 1
    end
  end,
  on_use = function (self, room, skillUseEvent, card, params)
    local player = skillUseEvent.from
    local cards = skillUseEvent.cards
    if #cards == 0 then return end
    if table.contains(player:getPile("$emo__shaoguang"), cards[1]) then
      local c = Fk:getCardById(cards[1])
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, skel.name, true, player)
      if player.dead then return end
      if c.type == Card.TypeBasic or c:isCommonTrick() then
        room:askToUseVirtualCard(player, {
          name = c.name, skill_name = skel.name, prompt = "#emo__yushi-use:::" .. c.name,
        })
      end
    else
      local use = {
        from = player,
        card = card,
        tos = skillUseEvent.tos,
      }
      return use
    end
  end,
  enabled_at_play = function (self, player)
    return #player:getPile("$emo__shaoguang") > 0
  end,
  enabled_at_response = function (self, player, response)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYin then return false end -- 阴状态直接不给发得了
    if not response and Fk.currentResponsePattern then
      for _, id in ipairs(player:getPile("$emo__shaoguang")) do
        local card = Fk:getCardById(id)
        if Exppattern:Parse(Fk.currentResponsePattern):matchExp(".|.|.|.|" .. card.name) then
          return true
        end
      end
    end
  end,
})

return skel
